﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System;
using System.Text;
using FileManager.Files;

namespace FileManager
{
    public partial class FileList : UserControl
    {
        private FileUploadPopup FileUploadPopupWindow = new FileUploadPopup();

        #region DependencyProperty HighlightedFilesProperty
        public static readonly DependencyProperty HighlightedFilesProperty =
            DependencyProperty.Register(
            "HighlightedFiles",
            typeof(List<FileObject>),
            typeof(FileList),
            new PropertyMetadata(null,
                new PropertyChangedCallback(OnHighlightedFilesChanged)
                )
                );

        public List<FileObject> HighlightedFiles
        {
            get { return (List<FileObject>)GetValue(HighlightedFilesProperty); }
            set { SetValue(HighlightedFilesProperty, value); }
        }
        #endregion

        #region FilesInList
        public WrapPanel FilesInList
        {
            get { return (WrapPanel)this.LayoutRoot; }
            set { this.LayoutRoot = (WrapPanel)value; }
        }
        #endregion

        public FileList()
        {
            InitializeComponent();
            FileUploadPopupWindow.Closed += new EventHandler(FileUploadPopupWindow_Closed);

        }

        #region OnHighlightedFilesChanged
        private static void OnHighlightedFilesChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            FileList objFileList = (FileList)d;
            List<FileObject> colHighlightedFiles = (List<FileObject>)(e.NewValue as List<FileObject>);

            objFileList.HighlightedFiles = colHighlightedFiles;
        }
        #endregion



        private void LayoutRoot_Drop(object sender, DragEventArgs e)
        {


            if (e.Data != null && e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

                if (files != null)
                {

                    try
                    {

                        var mainWindow = (MainPage)App.Current.RootVisual;
                        
                        var selectedFolderId = ((Folder)mainWindow.FolderTree.SelectedItem).FolderID;

                        if (selectedFolderId > -1)
                        {
                            FileUploadPopupWindow.Show();


                            foreach (var file in files)
                            {

                                StringBuilder AdditionalParamaters = new StringBuilder();
                                AdditionalParamaters.Append("?");
                                AdditionalParamaters.Append("p=" + mainWindow.intPortalID.ToString());
                                AdditionalParamaters.Append("&m=" + mainWindow.intModuleID.ToString());
                                AdditionalParamaters.Append("&u=" + mainWindow.intUserID.ToString());
                                AdditionalParamaters.Append("&f=" + selectedFolderId.ToString());
                                AdditionalParamaters.Append("&tp=" + mainWindow.strPassword);

                                Uri uri = new Uri(mainWindow.strWebServiceBase + "FileUpload.ashx" + AdditionalParamaters.ToString(), UriKind.Absolute);
                                FileUploadPopupWindow.uploadControl.UploadUrl = uri;
                                FileUploadPopupWindow.uploadControl.UploadChunkSize = 4194304;
                                FileUploadPopupWindow.uploadControl.ResizeImage = false;
                                //FileUploadPopupWindow.uploadControl.files.Clear();
                                
                                // Add the file to upload
                                DC.FileUpload.FileUpload objFileUpload = new DC.FileUpload.
                                    FileUpload(FileUploadPopupWindow.Dispatcher, uri, file);

                                FileUploadPopupWindow.uploadControl.files.Add(objFileUpload);
                                //FileUploadPopupWindow.uploadControl.UploadFiles();

                            }

                            foreach (var item in FileUploadPopupWindow.uploadControl.files)
                            {
                                item.Upload();
                               
                            }


                        }
                        else
                        {
                            var cw = new ChildWindow();
                            cw.Content = "Cannot drop files into a local folder.";
                            cw.Show();
                        }
                        e.Handled = true;
                        return;
                    }
                    catch (Exception ex)
                    {
                        var cw = new ChildWindow();
                        cw.Content = ex.Message;
                        cw.Show();
                    }

                }
            }

        }

        void FileUploadPopupWindow_Closed(object sender, EventArgs e)
        {
            var mainWindow = (MainPage)App.Current.RootVisual;
            //FileUploadPopupWindow.uploadControl.files.Clear();

            //?Fix for loosing currently uploading items?
            try
            {
                foreach (var item in FileUploadPopupWindow.uploadControl.files)
                {
                    if (item.Status == DC.FileUpload.FileUploadStatus.Complete)
                        FileUploadPopupWindow.uploadControl.files.Remove(item);
                }
            }
            catch 
            {
                // This is a temporary fix
                // obviously this is not good coding!
            }

            mainWindow.DisplayFilesInFolder();
        }
    
    }
}
